grep
Introduction
This cheat sheet provides a quick reference for some common grep
(Global Regular Expression Print) commands and concepts. grep
is a command-line utility used for searching and matching text patterns within files on Unix-like operating systems.
grep
Concepts
Basic Usage
grep
searches for a specified pattern in text files.
Search for a pattern in a file:
grep pattern file
Search for a pattern in multiple files:
grep pattern file1 file2
Regular Expressions
grep
supports regular expressions for advanced pattern matching.
Search using a basic regular expression:
grep -G pattern file
Search using an extended regular expression:
grep -E pattern file
Case Insensitive Search
grep
can perform case-insensitive searches.
- Perform a case-insensitive search:
grep -i pattern file
Invert Match
grep
can invert the match, displaying lines that do not contain the pattern.
- Display lines that do not contain the pattern:
grep -v pattern file
Line Numbers
grep
can display line numbers for matched lines.
- Display line numbers for matched lines:
grep -n pattern file
Recursive Search
grep
can perform recursive searches in directories.
- Recursively search for a pattern in directories:
grep -r pattern directory
Count Matches
grep
can count the number of matches.
- Count the number of matches:
grep -c pattern file
grep
Command-Line
Search for a pattern in a file:
grep pattern file
Search for a pattern case-insensitively:
grep -i pattern file
Display line numbers for matched lines:
grep -n pattern file
Recursively search for a pattern in directories:
grep -r pattern directory
Count the number of matches:
grep -c pattern file
Conclusion
This cheat sheet covers some common grep
(Global Regular Expression Print) commands and concepts. grep
is a powerful tool for searching and matching text patterns within files, making it useful for tasks like log analysis and text processing; refer to the official grep
documentation for more in-depth information and advanced usage.